package org.goyo.main.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.goyo.common.baseBean.AjaxResult;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.JSONUtil;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.entity.system.SysDepGroup;
import org.goyo.main.services.system.ISysDepGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sysDepGroup")
public class SysDepGroupController {
  protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	private ISysDepGroupService sysDepGroupService;
	/**
	 * 资源list
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/toSysDepGroupList")
	public String toSysDepGroupList(HttpServletRequest request,HttpServletResponse response,Model model){
		List<SysDepGroup> list = null;
		try {
			list = sysDepGroupService.getAllSysDepGroup();
			model.addAttribute("list", JSONUtil.obj2Json(list));
		} catch (Exception e) {
		  logger.error(e.getMessage(),e);
        }
		return "/system/sysDepGroupList";
	}
	
	@RequestMapping("/addSysDepGroup")
    @ResponseBody
	public String addSysDepGroup(HttpServletRequest request,HttpServletResponse response,Model model){
	  Map<String,String> map = new HashMap<String, String>();
	  try {
	    map.put("sn", sysDepGroupService.getDepGroupSerialNumber());
	    return JSONUtil.obj2Json(map);
    } catch (DBException | ServiceException e) {
      logger.error(e.getMessage(),e);
    }
	  return  null;
	}
	
	/**
	 * 资源保存
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/saveOrUpdateSysDepGroup")
	@ResponseBody
	public AjaxResult saveOrUpdateSysDepGroup( SysDepGroup sysDepGroup,HttpServletRequest request,HttpServletResponse response){
		
		AjaxResult result = null;
		try {
		    String pid = sysDepGroup.getParentId();
			String uuid = sysDepGroupService.saveOrUpdateInfo(sysDepGroup);
			List<SysDepGroup> list = sysDepGroupService.getAllSysDepGroup();
			result = AjaxResult.resultSuccess("保存成功", list,uuid);
		} catch (Exception e) {
		  logger.error(e.getMessage(),e);
		  result = AjaxResult.resultFail("保存失败,请确认编号唯一。", null);
		}
		return result;
	}
	/**
	 * 资源删除
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/delSysDepGroup")
	@ResponseBody
	public AjaxResult delSysDepGroup(HttpServletRequest request){
		AjaxResult result = null;
		try {
			String uuid = request.getParameter("uuid");
			sysDepGroupService.delByUuids(new String[]{uuid});
			List<SysDepGroup> list = sysDepGroupService.getAllSysDepGroup();
			result = AjaxResult.resultSuccess("删除成功", list);
		} catch (Exception e) {
			result = AjaxResult.resultFail("删除失败", null);
		  logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	   @RequestMapping("/listData")
	    @ResponseBody
	    public String getListData(HttpServletRequest request,Model model){
	        
	        JSONObject data = null;
	        //获取条件
	        //Map<String, Object> cond = JSONUtil.json2Map(request.getParameter("cond"));
	        //分页条件
	        int page = Integer.parseInt(request.getParameter("page"));
	        int pageSize = Integer.parseInt(request.getParameter("pagesize"));
	        String sortname = request.getParameter("sortname");
	        String sortorder = request.getParameter("sortorder");
	        String leaf = request.getParameter("leaf");
	        
	        String where = request.getParameter("where"); 
	        
	        if(leaf != null && !"".equals(leaf)){
	          if(where != null && !"".equals(where)){
	              where = where + "and leaf = '"+leaf+"'";
	          }else{
	              where ="leaf = '"+leaf+"'";
	          }
	      }
	        //分页器
	        Pager pager = Pager.convert(page, pageSize, sortname, sortorder);
	        try {
	            pager = sysDepGroupService.findByCondition(where, pager);
	            Map<String,Object> map = new HashMap<String,Object>();
	            map.put("Rows", pager.getList());
	            map.put("Total", pager.getCount());
	            data = JSONObject.fromObject(map);
	        }catch (Exception e) {
	          logger.error(e.getMessage(),e);
	        } 
	        return data.toString();
	    }
}
